<link rel="stylesheet" href="/assets/store/plugins/kindeditor/themes/default/default.css">
<link rel="stylesheet" href="/assets/store/plugins/tagsinput/amazeui.tagsinput.css">
<style>
.am-form .am-tagsinput {
    min-width: 100%;
  }
</style>
<div class="row-content am-cf">
    <div class="row">
        <div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
            <div class="widget am-cf">
                <form id="my-form" class="am-form tpl-form-line-form" method="post">
                    <input type="hidden" name="doc[id]" value="<?php echo $info['id']; ?>" />
                    <div class="widget-body">
                        <fieldset>
                            <div class="widget-head am-cf">
                                <div class="widget-title am-fl">文章信息-编辑</div>
                            </div>
                            <div class="am-form-group">
                                <label class="am-u-sm-2 am-form-label form-require">标题 </label>
                                <div class="am-u-sm-10 am-u-end">
                                    <input type="text" class="tpl-form-input" name="doc[title]"
                                           value="<?php echo $info['title']; ?>" required>
                                </div>
                            </div>

                            <div class="am-form-group">
                                <label class="am-u-sm-2 am-form-label form-require">栏目 </label>
                                <div class="am-u-sm-10 am-u-end">
                                    <label class="am-form-label" style="color:red;">
                                        <input type="hidden" name="doc[category_id]" value="<?php echo $category['id']; ?>" />
                                        <?php echo $category['title']; ?>
                                    </label>
                                </div>
                            </div>

                            <div class="am-form-group">
                                <label class="am-u-sm-2 am-form-label form-require">类型 </label>
                                <div class="am-u-sm-10">
                                    <label class="am-radio-inline">
                                        <input type="radio" name="doc[atype]" value="1" <?php echo $info['atype']==1 ? 'checked=""' : ''; ?> data-am-ucheck="" class="am-ucheck-radio"><span class="am-ucheck-icons"><i class="am-icon-unchecked"></i><i class="am-icon-checked"></i></span>
                                        普通
                                    </label>
                                    <label class="am-radio-inline">
                                        <input type="radio" name="doc[atype]" value="2" <?php echo $info['atype']==2 ? 'checked=""' : ''; ?> data-am-ucheck="" class="am-ucheck-radio"><span class="am-ucheck-icons"><i class="am-icon-unchecked"></i><i class="am-icon-checked"></i></span>
                                        焦点
                                    </label>
                                </div>
                            </div>

                            <div class="am-form-group">
                                <label class="am-u-sm-2 am-form-label form-require">图片 </label>
                                <div class="am-u-sm-10 am-u-end">
                                    <input type="hidden" id="image-url" name="doc[pic]" value="<?php echo $info['pic']; ?>" required>
                                    <div class="am-form-file">
                                        <div class="am-form-file">
                                            <button type="button"
                                                    class="upload-file am-btn am-btn-secondary am-radius">
                                                <i class="am-icon-cloud-upload"></i> 选择图片
                                            </button>
                                            <div class="uploader-list am-cf">
                                                <?php if(!empty($info['pic'])): ?>
                                                    <div class="file-item"><img src="<?php echo $info['pic']; ?>" /></div>
                                                <?php endif; ?>
                                            </div>
                                        </div>
                                        <div class="help-block am-margin-top-sm">
                                            <small>尺寸750x750像素以上，大小10M以下</small>
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <div class="am-form-group">
                                <label class="am-u-sm-2 am-form-label form-require">标签 </label>
                                <div class="am-u-sm-10 am-u-end">
                                    <input id="tags-1" type="text" name="doc[tags]" value="<?php echo $info['tags']; ?>" data-role="tagsinput"/>
                                </div>
                            </div>

                            <div class="am-form-group">
                                <label class="am-u-sm-2 am-form-label">描述 </label>
                                <div class="am-u-sm-10 am-u-end">
                                    <!-- 加载编辑器的容器 -->
                                    <textarea id="desc" name="doc[desc]"><?php echo $info['desc']; ?></textarea>
                                </div>
                            </div>

                            <div class="am-form-group">
                                <label class="am-u-sm-2 am-form-label form-require">作者 </label>
                                <div class="am-u-sm-10 am-u-end">
                                    <input type="text" class="tpl-form-input" name="doc[author]"
                                           value="<?php echo $info['author']; ?>" required>
                                </div>
                            </div>

                            <div class="am-form-group">
                                <label class="am-u-sm-2 am-form-label form-require">浏览量 </label>
                                <div class="am-u-sm-10 am-u-end">
                                    <input type="text" class="tpl-form-input" name="doc[view]"
                                           value="<?php echo $info['view']; ?>" required>
                                </div>
                            </div>

                            <div class="am-form-group">
                                <label class="am-u-sm-2 am-form-label form-require">内容 </label>
                                <div class="am-u-sm-10 am-u-end">
                                    <!-- 加载编辑器的容器 -->
                                    <textarea id="container" name="doc[content]" style="height:350px;"><?php echo htmlspecialchars_decode($info['content']); ?></textarea>
                                </div>
                            </div>


                            <div class="am-form-group">
                                <div class="am-u-sm-9 am-u-sm-push-3 am-margin-top-lg">
                                    <button type="submit" class="j-submit am-btn am-btn-secondary">编辑
                                    </button>
                                    
                                    <a href="javascript:;;" class="am-btn am-btn-warning analyse-btn" style="margin-left:10px;">分析内容</a>

                                    <a href="javascript:;;" class="am-btn am-btn-success downimg-btn" style="margin-left:10px;">下载图片</a>
                                </div>
                            </div>
                        </fieldset>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<script src="/assets/store/plugins/kindeditor/kindeditor-min.js"></script>
<script src="/assets/store/plugins/kindeditor/lang/zh_CN.js"></script>
<script src="/assets/store/plugins/tagsinput/amazeui.tagsinput.min.js"></script>
<script>
    $(function () {
        //$('#tags-1').tagsinput('add', '联赛');
        var editor = KindEditor.create('#container' , TEXT_EDITOR_CONFIG);//内容框
        var imgeditor = KindEditor.editor(IMG_EDITOR_CONFIG);//图片上传配置
        /**
         * 上传图片
         */
        $('.upload-file').click(function(){
            imgeditor.loadPlugin('image', function() {
                imgeditor.plugin.imageDialog({
                    imageUrl : $('#image-url').val(),
                    clickFn : function(url, title, width, height, border, align) {
                        url = url.indexOf('http')>-1 ? url : '<?php echo config('myconfig.host_url'); ?>' + url.slice(1);
                        $('#image-url').val(url);
                        $('.uploader-list').html('<div class="file-item"><img src="'+url+'" /></div>');
                        imgeditor.hideDialog();
                    }
                });
            });
        });
        /**
         * 分析内容
         */
        $('.analyse-btn').click(function(){
            var ii = layer.load(0,{shade: [0.8,'#000']});
            var content = editor.html();
            $.when($.ajax({
                type : 'post',
                url : '<?php echo url('util/title'); ?>',
                data : { content : content  }
            }) , $.ajax({
                type : 'post',
                url : '<?php echo url('util/keywords'); ?>',
                data : { content : content  }
            }) , $.ajax({
                type : 'post',
                url : '<?php echo url('util/desc'); ?>',
                data : { content : content  }
            }) , $.ajax({
                type : 'post',
                url : '<?php echo url('util/rewrite'); ?>',
                data : { content : content  }
            })).then(function(tdata , kdata , ddata , rdata){
                var titledata = tdata[0] , kwdata = kdata[0] , descdata = ddata[0] , redata = rdata[0];
                msg = "";
                if(titledata.code > 0){
                    $('#title').val(titledata.data.title);
                }
                msg += titledata.msg;
                if(kwdata.code > 0){
                    var tags = kwdata.data.tags;
                    for(var i in tags){
                        $('#tags-1').tagsinput('add', tags[i]);
                    }
                }
                msg += ','+kwdata.msg;
                if(descdata.code > 0){
                    $('#desc').text(descdata.data.desc);
                }
                msg += ','+descdata.msg;
                if(redata.code > 0){
                    editor.html(redata.data.redata);
                }
                msg += ','+redata.msg;
                layer.msg(msg);
                layer.close(ii);
            });
            
        });
        /**
         *下载文档中的图片并设置第一张图为主图
         */
        $('.downimg-btn').click(function(){
            var ii = layer.load(0,{shade: [0.8,'#000']});
            var content = editor.html();
            //2.下载图片并添加水印
            var imgs = [];
            $(content).find('img').each(function(){
                imgs.push($(this).attr('src'));
            });
            $.post('<?php echo url('util/downimg'); ?>' , {imgs : imgs.join(',')} , function(data){
                console.log(data);
                if(data.code > 0){
                    var dest_data = data.data.imgdata;
                    for(var i in dest_data){
                        content = content.replace(dest_data[i].src , dest_data[i].dest);
                    }
                    editor.html(content);
                    var imgurl = $(content).find('img').eq(0).attr('src');
                    $('#image-url').val(imgurl);
                    $('.uploader-list').html('<div class="file-item"><img src="'+imgurl+'" /></div>');
                }
                layer.msg(data.msg);
                layer.close(ii);
            });
        });

        /**
         * 表单验证提交
         * @type {*}
         */
        $('#my-form').superForm({
            // form data
            buildData: function () {
                return {
                    doc: {
                        tags: $('#tags-1').val()
                    }
                };
            },
            // 自定义验证
            validation: function () {
                
                return true;
            }
        });

    });
</script>
